package com.snail.snailvr.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.snail.snailvr.VRApp;

import java.io.File;


/**
 * handle the app download over event
 */
public class ApkInstaller {

    /**
     * Add for Much Start 2015/10/15
     */
    //系统安装接口需要的安装标志
    private static final int FLAG_INSTALL_INTERNAL = 0x00000010;
    private static final int FLAG_INSTALL_EXTERNAL = 0x00000008;
    private static final int FLAG_INSTALL_REPLACE_EXISTING = 0x00000002;

    /**
     * Add for Much End 2015/10/15
     */

    private final long notificationId;
    private final String filePath;

    public ApkInstaller(Context context, long notificationId, String filePath, String pkgName) {
        this.notificationId = notificationId;
        this.filePath = filePath;
    }


    public synchronized void execute() {
        installWithSystem();
    }

    private void installWithSystem() {
        installApk(filePath, true, false, notificationId);
    }


    /**
     * 安装应用程序
     *
     * @param apkLocalUri APK文件的路径
     */
    public static void installApk(String apkLocalUri) {
        installApk(apkLocalUri, false);
    }

    /**
     * 安装应用程序
     *
     * @param apkLocalUri APK文件的路径
     */
    public static void installApk(String apkLocalUri, boolean isFilePath, boolean failde, long notificationId) {
        if (apkLocalUri == null) return;
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setDataAndType(isFilePath ? Uri.fromFile(new File(apkLocalUri)) : Uri.parse(apkLocalUri),
                "application/vnd.android.package-archive");
        VRApp.getInstance().startActivity(i);
    }

    /**
     * 安装应用程序
     *
     * @param apkLocalUri APK文件的路径
     */
    public static void installApk(String apkLocalUri, boolean isFilePath) {
        installApk(apkLocalUri, isFilePath, false, -1);
    }


}